package com.funo.logic.generate.code;

import cn.hutool.core.collection.CollectionUtil;
import com.funo.logic.constant.CodeConstants;
import com.funo.logic.constant.Modifier;
import com.funo.logic.constant.SymbolConstants;

import java.util.List;

public abstract class AbstractCodeSupport implements CodeSupport {

    protected StringBuffer codeBuffer = new StringBuffer();

    @Override
    public void addBodyLine(String line) {
        codeBuffer.append(line);
    }

    @Override
    public void addBodyLines(List<String> lines) {
        codeBuffer.append(lines);
    }

    @Override
    public void appendLine(String line) {
        codeBuffer.append(line);
    }

    /**
     * 换行
     */
    @Override
    public void addNewLine() {
        codeBuffer.append(SymbolConstants.LINE_BREAK);
    }

    /**
     * 添加封号
     */
    @Override
    public void addSemicolon() {
        codeBuffer.append(SymbolConstants.SEMICOLON);
    }

    /**
     * 添加一个空格
     */
    @Override
    public void addSpace() {
        codeBuffer.append(SymbolConstants.SPACE);
    }

    /**
     * 添加左大括号
     */
    @Override
    public void addOpenBrace() {
        codeBuffer.append(SymbolConstants.OPEN_BRACE);
    }

    /**
     * 添加右大括号
     */
    @Override
    public void addCloseBrace() {
        codeBuffer.append(SymbolConstants.CLOSE_BRACE);
    }

    /**
     * 添加左小括号
     */
    @Override
    public void addOpenParen() {
        codeBuffer.append(SymbolConstants.OPEN_PAREN);
    }

    /**
     * 添加右小括号
     */
    @Override
    public void addCloseParen() {
        codeBuffer.append(SymbolConstants.CLOSE_PAREN);
    }

    /**
     * 添加单边双引号
     */
    @Override
    public void addQuotation() {
        codeBuffer.append(SymbolConstants.QUOTATION);
    }

    @Override
    public void addOpenAngle() {
        codeBuffer.append(SymbolConstants.OPEN_ANGLE);
    }

    @Override
    public void addCloseAngle() {
        codeBuffer.append(SymbolConstants.CLOSE_ANGLE);
    }

    /**
     * 添加Tab缩进
     */
    @Override
    public void addIndent() {
        addIndent(CodeConstants.INDENT);
    }

    @Override
    public void addIndent(int indent) {
        for (int i = 0; i < indent; i++) {
            addSpace();
        }
    }

    protected boolean isPublic(List<Modifier> modifiers) {
        return isModifierMatch(modifiers, Modifier.PUBLIC);
    }

    protected boolean isProtected(List<Modifier> modifiers) {
        return isModifierMatch(modifiers, Modifier.PROTECTED);
    }

    protected boolean isPrivate(List<Modifier> modifiers) {
        return CollectionUtil.isEmpty(modifiers) || isModifierMatch(modifiers, Modifier.PRIVATE);
    }

    protected boolean isAbstract(List<Modifier> modifiers) {
        return isModifierMatch(modifiers, Modifier.ABSTRACT);
    }

    protected boolean isStatic(List<Modifier> modifiers) {
        return isModifierMatch(modifiers, Modifier.STATIC);
    }

    protected boolean isFinal(List<Modifier> modifiers) {
        return isModifierMatch(modifiers, Modifier.FINAL);
    }

    protected boolean isModifierMatch(List<Modifier> modifiers, Modifier modifier) {
        return modifiers.stream().anyMatch(modifier::equals);
    }

    @Override
    public void addPoint() {
        codeBuffer.append(SymbolConstants.POINT);
    }
}
